#!/bin/bash
CUR_DIR=$(cd $(dirname $0) ; pwd)
UPLOAD_DIR=${UPLOAD_DIR-${CUR_DIR}}
LOG_PATH=${CUR_DIR}/data_collect
LOG_FILE=${LOG_PATH}/runlog.txt
HOSTNAME=$(uname -n)
CONF_PATH="/etc/oe_log_collect"

function echolog()
{
    echo "$@" | tee -a ${LOG_FILE}
}

function main()
{
    # check the oe_log_collect python pkg is installed or not

    pip3 install "${CUR_DIR}/oe_log_collect-"*.whl

    [[ -d "${CONF_PATH}" ]] || {
        oe_log_collect --init
    }

    oe_log_collect --output "${LOG_PATH}" --no_compressed

    if [[ ! -d ${UPLOAD_DIR} ]]; then
        mkdir -p ${UPLOAD_DIR}
    fi

    echolog "cp -rf ${LOG_PATH} ${UPLOAD_DIR}"
    cp -rf ${LOG_PATH}/${HOSTNAME}_*/* ${UPLOAD_DIR}/
    if [  $? -ne 0 ];then
        echolog "cp failed"
        return 1
    fi
}

main "$@"
exit $?
